package org.easydev.wechat.client.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;
import org.easydev.wechat.client.WechatClient;
import org.easydev.wechat.client.handle.WechatRequestHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

/**
 * <Description> <br>
 * 
 * @author jian<br>
 * @version 1.0<br>
 */
public class WechatReqUtil {

    /**
     * 微信默认请求类型
     */
    public static final String DEFAULT_HTTP_METHOD = "GET";

    /**
     * 微信默认处理handle
     */
    public static final String DEFAULT_MAPPINGHANDLER = "org.easydev.wechat.client.handle.HandleDefaultRequest";

    /**
     * 默认参数类型
     */
    public static final String DEFAULT_DATATYPE = "PARAM";

    /**
     * LOGGER
     */
    private static Logger LOGGER = LoggerFactory.getLogger(WechatClient.class);

    /**
     * 缓存请求配置
     */
    private static Map<String, WechatReqConfig> REQ_MAPPING = new HashMap<String, WechatReqConfig>();

    /**
     * 缓存处理请求handler
     */
    private static Map<String, WechatRequestHandle> MAPPING_HANDLER = new HashMap<String, WechatRequestHandle>();

    /**
     * 通过名称获取对象
     * 
     * @param className
     * @return
     */
    private static Object getObjectByClassName(String className) {
        try {
            return Class.forName(className).newInstance();
        } catch (Exception e) {
            LOGGER.error("[通过名称获取对象异常] className: ".concat(className), e);
        }
        return null;
    }

    /**
     * 获取配置信息
     */
    public static WechatReqConfig getWechatConfig(String key) {
        return REQ_MAPPING.get(key);
    }

    /**
     * 服务请求处理
     */
    public static WechatRequestHandle getMappingHander(String className) {
        WechatRequestHandle handler = WechatReqUtil.MAPPING_HANDLER.get(className);
        if (handler == null) {
            handler = (WechatRequestHandle) getObjectByClassName(className);
            MAPPING_HANDLER.put(className, handler);
        }
        return handler;
    }

    /**
     * Description: 微信服务注册<br>
     * 
     * @param configName <br>
     */
    public static void registService(String configName) {
        File file = new File(WechatReqUtil.class.getClassLoader().getResource(configName).getPath());
        Scanner scanner = null;
        try {
            scanner = new Scanner(file, "utf-8");
            StringBuilder buffer = new StringBuilder();
            while (scanner.hasNextLine()) {
                buffer.append(scanner.nextLine());
            }
            if (StringUtils.isBlank(buffer)) {
                LOGGER.error("file is empty :".concat(configName));
                return;
            }
            JSONObject ServiceConfig = JSONObject.parseObject(buffer.toString());
            List<WechatReqConfig> wechatConfigList = JSONObject.parseArray(ServiceConfig.getString("wechatService"), WechatReqConfig.class);
            if (null != wechatConfigList && wechatConfigList.size() > 0) {
                for (WechatReqConfig config : wechatConfigList) {
                    REQ_MAPPING.put(config.getKey(), config);
                }
            }
        } catch (FileNotFoundException e) {
            LOGGER.error("file not find ".concat(configName), e);
        } catch (Exception e) {
            LOGGER.error("regist service error: ".concat(configName), e);
        } finally {
            if (null != scanner) {
                scanner.close();
            }
        }
    }

}
